package com.example.xd.commen.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * @auther XuDan on 2019/7/1.
 */
public class PrivilegeProcess {
    /**
     * 注解在成员变量上
     *
     * @param path
     * @return
     */
    public static String[] getPermissionsField(String path) {
        String[] value = null;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(path);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Field[] fields = clazz.getDeclaredFields();
        for (final Field field : fields) {
            if (field.isAnnotationPresent(Privilege.class)) {//是否使用Privilege注解
                for (Annotation annotation : field.getDeclaredAnnotations()) {//获得所有的注解
                    if (annotation.annotationType().equals(Privilege.class)) {
                        field.setAccessible(true);
                        Privilege annotation1 = field.getAnnotation(Privilege.class);
                        value = annotation1.value();
                    }
                }
            }
        }
        return value;
    }

    /**
     * 注解在类上
     *
     * @param path
     * @return
     */
    public static String[] getPermissions(String path) {
        String[] value = null;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(path);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (clazz.isAnnotationPresent(Privilege.class)) {//是否使用Privilege注解
            Privilege annotation = clazz.getAnnotation(Privilege.class);
            value = annotation.value();
        }
        return value;
    }

    /**
     * 注解在类上
     *
     * @param clazz
     * @return
     */
    public static String[] getPermissions(Class clazz) {
        String[] value = null;
        if (clazz.isAnnotationPresent(Privilege.class)) {//是否使用Privilege注解
            Privilege annotation = (Privilege) clazz.getAnnotation(Privilege.class);
            value = annotation.value();
        }
        return value;
    }


    public static boolean isElementNoDefaultValue(String typeName) {
        return (String.class.getName().equals(typeName) || typeName.contains("[]") || typeName.contains("Bundle"));
    }
}
